<template>
  <div class="publish-content">
    <div class="publish-title">
      <span @click="hanldeCancel">取消</span>
      <div>写说说</div>
      <!-- disabled： true => 禁用  false => 可用 -->
      <button @click="release" :disabled="disabled" :class="disabled ? 'isDisabled' : ''">发布</button>
    </div>
    <!-- 线 -->
    <div class="van-hairline--top"></div>
    <!-- 编辑区域 -->
    <div class="edit-content">
      <van-form class="form-flex">
        <!-- 文本编辑 -->
        <van-field
          v-model="formInfo.text"
          rows="3"
          autosize
          type="textarea"
          placeholder="分享新鲜事..."
          class="text-edit"
          maxlength="200"
          show-word-limit
        />
        <!-- 图片上传 -->
        <van-field name="uploader">
          <template #input>
            <van-uploader
                v-model="formInfo.uploaderImgList"
                multiple
                :max-count="9"
                accept="image/png, image/jpeg, image/jpg"
                :after-read="afterRead"
                @delete="deletPhoto"
            />
          </template>
        </van-field>
      </van-form>
    </div>
    <!-- 线 -->
    <div class="van-hairline--top"></div>
  </div>
</template>

<script>
import { reqSpaceUploadPhotos, reqSpaceUploadPhoto, reqAddSpace } from '@/api'
import { mapState } from 'vuex'
import { Toast } from 'vant'
export default {
  data() {
    return {
      formInfo: {
        text: "",
        uploaderImgList: []
      },
      photoList: []
    }
  },
  computed: {
    ...mapState({
        userId: state => state.login.userInfo._id
    }),
    disabled() {
        if (this.formInfo.text === '' && this.photoList.length === 0) {
            return true
        } else {
            return false
        }
    }
  },
  methods: {
    // 取消按钮
    hanldeCancel() {
        this.formInfo.text = ''
        this.formInfo.uploaderImgList = []
        this.photoList = []
        this.$emit('cancel', false)
    },
    // 发布按钮
    async release() {
        let obj = {
            userId: this.userId,
            textDesc: this.formInfo.text,
            photos: this.photoList
        }
        let res = await reqAddSpace(obj)
        if (res.status === 200) {
            this.hanldeCancel()
            this.$emit('onRefresh')
        }
        Toast(res.msg)
    },
    // 图片删除
    deletPhoto(file, index) {
        this.photoList.splice(index.index, 1)
    },
    // 图片上传
    async afterRead(file) {
        if (!file.length) {
            // 单图片上传
            file.status = 'uploading'
            file.message = '上传中...'
            let formData = new FormData()
            formData.append('photo', file.file)
            let res = await reqSpaceUploadPhoto(formData)
            if (res.res_code === 200) {
                file.status = 'done'
                file.message = '上传成功'
                this.photoList.push(res.url)
            } else {
                file.status = 'failed'
                file.message = '上传失败'
            }
        } else {
            // 多图片上传
            file.forEach(i => {
                i.status = 'uploading'
                i.message = '上传中...'
            })
            let formData = new FormData()
            //需要依次添加进去
            for (let i = 0; i < file.length; i++) {
                formData.append('photos', file[i].file)
            }
            let res = await reqSpaceUploadPhotos(formData)
            if (res.res_code === 200) {
                file.forEach(i => {
                    i.status = 'done'
                    i.message = '上传成功'
                })
                res.url.forEach(i => {
                    this.photoList.push(i)
                })
            } else {
                file.forEach(i => {
                    i.status = 'failed'
                    i.message = '上传失败'
                })
            }
        }
    }
  }
}
</script>

<style lang="less" scoped>
.publish-content {
  .publish-title {
    width: 100%;
    height: 1.2667rem;
    padding: 0.2667rem 0.2667rem 0.2667rem 0.3333rem;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.4267rem;
    span {
        font-size: .4rem;
    }
    div {
      margin-left: 0.5333rem;
    }
    button {
      width: 1.5333rem;
      height: 0.8rem;
      background-color: #4dc0ff;
      border: none;
      color: #fff;
      font-size: 0.3467rem;
      border-radius: 0.1333rem;
    }
    .isDisabled {
        background-color: #9bd9fb;
    }
  }
  .edit-content {
    width: 100%;
    box-sizing: border-box;
    .text-edit {
        width: 100%;
        min-height: 3.3333rem;
    }
  }
}
.form-flex {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}
/deep/ .van-uploader__upload {
    width: 2.6667rem;
    height: 2.6667rem;
}
</style>

<style>
.van-overlay {
  background-color: rgba(0, 0, 0, 0.3);
}
.van-cell::after {
    border-bottom: none;
}
</style>
